"""
接口路由 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz import api_route_biz
from components.audit_log import audit
from components.depends import admin_required
from components.global_param import get_user_id
from components.logger import log
from components.response import OKWithDataResponse
from models.request import (
    APIRouteDetailRequest,
    APIRouteListRequest,
    APIRoutePathField,
)
from models.response import (
    APIRouteDetailResponse,
    APIRouteListResponse,
)
from models.vo import (
    APIRouteDetailVO,
    APIRouteListPage,
)
from utils.data_utils.special import ObjectIdStr

api_route_router: APIRouter = APIRouter(prefix="/api_route")
""" 接口路由 路由 """


@api_route_router.get(
    path="/{route_id}/",
    summary="接口路由 | 详情",
    response_model=APIRouteDetailResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.API_ROUTE,
    operation=enum.ModelOperation.DETAIL,
    po_id="route_id",
)
async def api_route_detail(route_id: ObjectIdStr = APIRoutePathField.route_id) -> OKWithDataResponse:
    request = APIRouteDetailRequest(route_id=route_id)
    vo: APIRouteDetailVO = api_route_biz.APIRouteGetter.detail(request)
    log.info(f"[接口路由][详情] user_id: {get_user_id()} route_id: {route_id}")

    return OKWithDataResponse(vo)


@api_route_router.get(
    path="/",
    summary="接口路由 | 列表",
    response_model=APIRouteListResponse,
    dependencies=[Depends(admin_required)],
)
@audit(
    model=enum.Model.API_ROUTE,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.route_id",
)
async def api_route_list(request: APIRouteListRequest = Depends(APIRouteListRequest.from_query)) -> OKWithDataResponse:
    vo: APIRouteListPage = api_route_biz.APIRouteGetter.list(request)
    log.info(f"[接口路由][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)
